import pygame

player1 = "player"
player2 = "AI"
players = [player1, player2]
current_player = players[0]
turn = 0
first_take_choice = -1
last_move = None
done = False
won = False
winner = None

# 初始化 Pygame 库
pygame.init()

# 显示设置
size = [600, 600]
green = (0, 155, 0)
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode(size)

# 游戏名字
pygame.display.set_caption("Solitaire Game")

# 棋盘的位置数据
POSITION = {
    1: (300, 50),
    2: (417, 118),
    3: (440, 250),
    4: (365, 375),
    5: (235, 403),
    6: (125, 345),
    7: (81, 235),
    8: (105, 118),
    9: (205, 190),
    10: (300, 243)
}

# 定义棋子的类
class Piece:
    def __init__(self, pos_number, color=None, cx=0, cy=0, radius=20):
        self.pos_number = pos_number
        self.color = color or (255, 255, 255)
        self.cx = cx
        self.cy = cy
        self.radius = radius

    # 画棋子
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.cx, self.cy), self.radius)

    # 翻转棋子颜色
    def flip_color(self):
        if self.color == black:
            self.color = white
        else:
            self.color = black

    # 移动棋子
    def move(self, dest):
        global last_move

        if last_move is not None:
            # 将 last_move 的颜色翻转
            last_move.flip_color()

        # 移动棋子的方法
        self.x, self.y = POSITION[dest]
        last_move = self

    # 判断是否可以跳到目标棋子
    def can_jump(self, target_piece):
        # 如果有棋子，则不能跳
        if(target_piece is not None):
            return False

        for adj in adjacent_pieces(self.pos_number):
            mid_piece = get_mid_piece(self.pos_number, adj)
            if(mid_piece is None):
                continue
            dest = get_dest(mid_piece.pos_number, self.pos_number, adj)
            if(target_piece == dest):
                return True
        return False

    # 判断棋子是否可以移动到某个位置
    def can_move_to(self, dest):
        # 如果目标已经有棋子，则不能移动
        if get_piece(dest) is not None:
            return False

        for adj in adjacent_pieces(self.pos_number):
            mid_piece = get_mid_piece(self.pos_number, adj)
            # 如果有棋子，则不能跳
            if(mid_piece is None):
                continue
            _dest = get_dest(mid_piece.pos_number, self.pos_number, adj)
            if(dest == _dest):
                return True
        return False

# 得到目标棋子位置
def get_dest(mid, start, end):
    if(mid == start):
        return end
    else:
        return start

# 获取相邻的棋子
def adjacent_pieces(pos_number):
    ADJACENT = {
        1: [2, 9],
        2: [1, 3],
        3: [2, 4],
        4: [3, 5],
        5: [4, 6],
        6: [5, 7],
        7: [6, 8],
        8: [7, 9],
        9: [1, 10, 8],
        10: [9, 5],
    }
    return ADJACENT[pos_number]

# 获取中间的棋子
def get_mid_piece(start, end):
    mid = ((start + end) // 2)
    return get_piece(mid)

# 获取某个位置的棋子
def get_piece(pos_number):
    for piece in pieces:
        if piece is not None and piece.pos_number == pos_number:
            return piece
    return None

# 画出棋盘
def draw_board():
    # 填充背景色为绿色
    screen.fill(green)

    # 定义五角星顶点的坐标
    star_points = [
        (300, 0), (330, 150), (480, 150), (360, 240), (390, 390), 
        (300, 300), (210, 390), (240, 240), (120, 150), (270, 150),
    ]
    
    # 绘制五角星，使用 pygame.draw.polygon 函数
    pygame.draw.polygon(screen, black, star_points, 3)

    # 在五角星的五个顶点和五个交叉点上放置棋子
    pos_numbers = [1,5,2,9,3,8,4,7,6,10]
    star_points = [
        (300, 0), (330, 150), (480, 150), (360, 240), (390, 390),
        (300, 300), (210, 390), (240, 240), (120, 150), (270, 150),
        ]
    for i in range(len(pos_numbers)):
        pos_number = pos_numbers[i]
        cx, cy = star_points[i]
        radius = 20
        color = black if pos_number != 10 else None
        piece = Piece(pos_number, color, cx, cy, radius)
        pieces.append(piece)
        piece.draw()

    # 添加文本显示
    font = pygame.font.Font(None, 36)
    if done and not won:
        text = font.render(f"游戏结束，你输了，棋子剩余{len(pieces)}颗", True, white)
    elif done and won:
        text = font.render(f"游戏结束，你赢了，棋子剩余{len(pieces)}颗", True, white)
    else:
        text = font.render(f"当前玩家：{current_player}", True, white)
    text_rect = text.get_rect(center=(300, 550))
    screen.blit(text, text_rect)

    pygame.display.flip()

# 初始化棋子
pieces = []

# 在五角星的五个顶点和五个交叉点上放置棋子
pos_numbers = [1,5,2,9,3,8,4,7,6,10]
star_points = [
    (300, 0), (330, 150), (480, 150), (360, 240), (390, 390), 
    (300, 300), (210, 390), (240, 240), (120, 150), (270, 150),
]
for i in range(len(pos_numbers)):
    pos_number = pos_numbers[i]
    cx, cy = star_points[i]
    radius = 20
    color = black if pos_number != 10 else None
    piece = Piece(pos_number, color, cx, cy, radius)
    pieces.append(piece)

# 定义玩家全局变量
player1 = "Player 1"
player2 = "Player 2"
current_player = player1  # 初始为玩家1

# 实现切换玩家的逻辑
def switch_player():
    global current_player  # 使用全局变量

    if current_player == player1:
        current_player = player2
    else:
        current_player = player1
